<?php
class Category_Form_QuickSearchCommercialForm extends Ac_Form
{
    private $_preview_link = '';

    private $_category;
    private $_adid;

    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
        //array(array('row' => 'HtmlTag'), array('tag' => 'button'))

        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class'=>'button_em')),
        //array('Description', array('placement' => 'APPEND', 'tag'=>'', 'escape' => false)),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
        //'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        //array('Label', array('placement' => 'prepend', 'id' => 'agentname_label')),
        //array(array('row' => 'HtmlTag'), array('tag' => 'p')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $formDecorators = array(
            //'FormElements',
            //array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
            //array('Description', array('placement' => 'prepend', 'tag'=>'', 'escape' => false)),
            //'fieldset',
            //'Form',
            'PrepareElements',
            array('ViewScript', array('viewScript' => 'index/customization/commercial.phtml'))
    );

    protected $subFormDecorators = array(
        'FormElements',
        //array('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form')),
        array('Description', array('placement' => 'prepend')),
        'fieldset',
    );

    protected $spanDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('placement' => 'prepend')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
        //array('HtmlTag', array('class' => 'inputText')),
    );

    protected $displayGroupDecorators = array(
        'FormElements',
        //array(array('outerHTmlTag' => 'HtmlTag'), array('tag' => 'p', 'class' => 'price radio text')),
        array('HtmlTag', array('tag' => 'p', 'class' => 'price radio text')),
        array('Description', array('placement' => 'prepend', 'tag' => 'label')),
    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
       // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'td', 'class' => 'element')),
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'span', 'class' => 'multiCheckbox')),
        array('Label', array('placement' => 'APPEND')),
    );

    protected $elementRadioDecorators = array(
        'ViewHelper',
        'Errors',
        // 'Description'
        //array(array('data' => 'HtmlTag'), array('tag' => 'span', 'class' => 'multiCheckbox')),
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'span')),
    );

    public function __construct($options = array())
    {
        $this->_preview_link = !empty($options['preview_link']) ? $options['preview_link'] : '';
        $this->_category = !empty($options['category']) ? $options['category'] : array();
        $this->_adid = !empty($options['adid']) ? $options['adid'] : array();
        parent::__construct($options);
    }

    public function init()
    {
        parent::init();

        $this->setMethod('get');
        $this->setAction($this->getView()->geturl('regular' ,'search'));

        $element = new Zend_Form_Element_Hidden('adid');
        $element->setValue((string) $this->_adid);
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        //Category
        $this->addElement('hidden', 'small_category_list', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'value' => 'commercial',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        /*
        //Sale By
        $this->addElement('radio', 'agency_owner_comm', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'multiOptions' => array(
                'Agency' => 'Agency',
                'Owner' => 'Owner',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementRadioDecorators,
            'required'   => true,
        ));
*/
        //Want to
        $this->addElement('radio', 'wantto_comm', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'Int'
            ),
            'multiOptions' => array(
                'Buy' => 'Buy',
                'Rent' => 'Rent',
            ),
            'separator' => ' ',
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
/*
        // -> Price
        $this->addElement('jqslider', 'price_comm', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 20000000,
                        'values'   => array('0', '20000000'),
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 500,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );
*/

        $this->addElement('textRange', 'price_comm', array(
                    'value'       => array(),
                    'class'       => array('class' => 'text-range'),
                    'decorators'  => array(
                            'Errors',
                            'ViewHelper',
                    )
                )
        );
/*
        $this->addElement('checkbox', 'sole_agency', array(
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'label' => 'Sole Agency',
            'decorators' => array(
                'ViewHelper',
                'Errors',
                array('Label', array('placement' => 'append')),
            ),
            'required'   => true,
        ));
*/
        /**********************************************************
         *                          AGENT
         *********************************************************/
/*
        $this->addElement('text', 'agent_name', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
        $this->addElement('text', 'agent_id', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        $this->addElement('text', 'agent_phone', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'int',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));

        $this->addElement('text', 'agent_email', array(
            'filters'    => array('StringTrim', 'StringToLower'),
            'validators' => array(
                'EmailAddress',
            ),
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => true,
        ));
*/
        /**********************************************************
         *                          LOCATION
         *********************************************************/
        $locations = array('All');
        $locs = Model_Location::all()->sort(array('path' => 1));
        $parent = '';
        foreach($locs as $k => $loc) {

            if (count($loc->path) == 1) {
                $parent = $loc->name;
                $locations[$loc->name] = array();
            } else {

                //$parent = (string) $loc->path[0];
                $tmp    = !empty($locations[(string) $parent]) ? $locations[(string) $parent] : array();

                $tmp[$k]         = (string) $loc->name;
                //echo '<pre>';
                //print_r($tmp);
                $locations[(string) $parent] = $tmp;
            }
        }

        //District
        $this->addElement('select', 'district_comm', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                'Alnum',
                array('StringLength', false, 24),
            ),
            'multiOptions' => $locations,
            'disableLoadDefaultDecorators' => true,
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));

        // -> Size
        $this->addElement('jqslider', 'size_comm', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 5000,
                        'values'   => array('0', '5000'),
                        'currency' => 'sqft',
                        'range'    => true,
                        'step'     => 500,
                        'change'   => 'refineSearch($(this))',
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                    )
                )
        );
/*
        // -> Building age
        $this->addElement('jqslider', 'building_age_comm', array(
                    'jQueryParams' => array(
                        'min'      => 0,
                        'max'      => 50,
                        'values'   => array('0', '50'),
                        'range'    => true,
                        'step'     => 1,
                        ),
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array(array('row' => 'HtmlTag'), array('tag' => 'p', 'class' => 'slide')),
                    )
                )
        );
*/
        // --> Keyword
        $this->addElement('text', 'keyword_comm', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators' => $this->elementDecorators,
            'required'   => false,
        ));

        $this->addElement('button', 'v', array(
            'required'    => false,
            'ignore'      => true,
            'type'        => 'submit',
            'label'       => 'Search',
            'decorators'  => $this->elementButtonDecorators,
        ));

    }

    public function __toString()
    {
        $str = '
            $j(document).ready(function() {
                $j("#area_comm").change(function (event) {
                    event.stopImmediatePropagation();
                    $j.get("/search/get-attribute", { c:"area", f:"district", id: $j("#area_comm").val() },
                        function(data) {
                            $j("#district_comm").empty();
                            $j.each(data, function (index, value) {
                                $(document.createElement("option"))
                                    .html(value)
                                    .attr("value", index)
                                    .appendTo($("#district_comm"));
                            });
                        });
               });
           });
        ';
        $this->view->JQuery()->addJavascript($str);
        return parent::__toString();
    }
}